app.factory("connectPro", ["$http", "$window", "$q", "$location", "$exception", "$mdDialog", "$token", "$canceler", "$indexeddb", function ($http, $window, $q, $location, $exception, $mdDialog, $token, $canceler, $indexeddb) {
    function ConnectProService() {
    };

    ConnectProService.prototype = {
        connect: function (model, shopId/*optional*/) {
            var self = this;
            return $http.post("/pro/connect", model, { params: { shopId: shopId }, timeout: $canceler.create().promise }).success(function (res) {
                return $token.set(res.token).then(function () {
                    $location.path(res.route);
                });
            }).catch(function (error) {
                if (status == 500 && error.shops) {
                    return self.selectShop(error.shops).then(function (shop) {
                        return self.connect(model, shop._id);
                    });
                }
                else $exception.show(error);
            });
        },
        disconnect: function () {
            return $token.reset().then(function () {
                return $indexeddb.clearAll();
            }).finally(function(){
                $window.location = "/pro";
            });
        },
        gotoPro: function (shopId/*optional*/) { 
            var self = this;
            return $http.post("/pro/connectfrom", {}, { params: { token: token, shopId: shopId }, timeout: $canceler.create().promise }).success(function (res) {
                return $token.set(res.token).then(function () {
                    $location.path(res.route);
                    $window.location = "/pro#" + res.route;
                });
            }).catch(function (error, status) {
                if (status == 500 && error.shops) {
                    return self.selectShop(error.shops).then(function (shop) {
                        self.gotoPro(shop._id);
                    });
                }
                else $exception.show(error);
            });
        },
        selectShop: function (shops) {

            function DialogController($scope, $mdDialog, shops) {
                $scope.shops = shops;
                $scope.shop = shops[0];

                $scope.hide = function () {
                    $mdDialog.hide();
                };

                $scope.cancel = function () {
                    $mdDialog.cancel();
                };

                $scope.answer = function (newValue) {
                    $mdDialog.hide(newValue);
                };
            };

            $mdDialog.show({
                controller: DialogController,
                templateUrl: '/pro/partials/connection/pro.html',
                locals: {
                    shops: shops
                }
            }).then(function(answer) {
                return scope.shop;
            }, function() {
            });
        }
    };
    return new ConnectProService();
} ]);
